/*
 * Created on 11 October 2006
 * author: Klarinda
 * 
 * Please refer to W3C XPath2.0 Grammar Test Page
 * (http://www.w3.org/2005/qt-applets/xpathApplet.html) 
 */

package dbsucxentW.pathProcessorW;

import dbsucxentW.constantW.Constant;
import dbsucxentW.xpathW.SimpleNode;

public class StepNodes {
	protected int _counter = 0;

	protected int _noOfChildren = 0;

	protected int _noOfSteps = 0;

	protected SimpleNode _PathExprNode;

	protected SimpleNode _currStepNode;

	protected int _currAxisType;

	// protected boolean _prevslashslash;

	public StepNodes(SimpleNode node) {
		_PathExprNode = node; // XPath2
		System.out.println("StepNodes " + _PathExprNode.toString());
		_PathExprNode = (SimpleNode) _PathExprNode.jjtGetChild(0); // XPath
		System.out.println("StepNodes " + _PathExprNode.toString());
		_PathExprNode = (SimpleNode) _PathExprNode.jjtGetChild(0); // Expr
		System.out.println("StepNodes " + _PathExprNode.toString());
		_PathExprNode = (SimpleNode) _PathExprNode.jjtGetChild(0); // PathExpr
		System.out.println("StepNodes " + _PathExprNode.toString());

		_counter = -1;
		_noOfChildren = _PathExprNode.jjtGetNumChildren();
		System.out.println("StepNodes no of children: " + _noOfChildren);
		int i;
		for (i = 0; i < _noOfChildren; i++) {
			if (_PathExprNode.jjtGetChild(i).toString().compareTo("StepExpr") == 0)
				_noOfSteps++;
		}
		System.out.println("StepNodes no of steps: " + _noOfSteps);
	}

	public SimpleNode nextStep() {
		_counter++;
		while (_PathExprNode.jjtGetChild(_counter).toString().compareTo(
				"StepExpr") != 0) {
			_counter++;
			if (_counter >= _noOfChildren)
				return null;
		}
		_currStepNode = (SimpleNode) _PathExprNode.jjtGetChild(_counter);
		System.out.println("StepNodes.nextStep(), " + _counter + ", "
				+ _currStepNode.toString());
		return _currStepNode;
	}

	public boolean isLast() {
		if (_counter >= _noOfChildren - 1)
			return true;
		else
			return false;
	}

	public SimpleNode getCurrNode() {
		return _currStepNode;
	}

	public SimpleNode getStepNodes() {
		return _PathExprNode;
	}

	public int getAxisType() {
		SimpleNode snode = (SimpleNode) _currStepNode.jjtGetChild(0);
		System.out.println("StepNodes.getAxisType() " + snode.toString() + " "
				+ snode.printValue());
		String axis = snode.printValue();

		if (snode.toString().compareTo("AbbrevForwardStep") == 0) {
			if (_counter > 0
					&& _PathExprNode.jjtGetChild(_counter - 1).toString()
							.compareTo("SlashSlash") == 0)
				_currAxisType = Constant.DESCENDANT;
			else
				_currAxisType = Constant.CHILD;
		} else if (axis == null) {
			_currAxisType = Constant.UNKNOWNAXIS;
		} else if (axis.compareTo("child") == 0) {
			_currAxisType = Constant.CHILD;
		} else if (axis.compareTo("following") == 0) {
			_currAxisType = Constant.FOLLOWING;
		} else if (axis.compareTo("preceding") == 0) {
			_currAxisType = Constant.PRECEDING;
		} else if (axis.compareTo("following-sibling") == 0) {
			_currAxisType = Constant.FOLLOWING_SIBLING;
		} else if (axis.compareTo("preceding-sibling") == 0) {
			_currAxisType = Constant.PRECEDING_SIBLING;
		} else if (axis.compareTo("attribute") == 0) { // ???
			_currAxisType = Constant.ATTRIBUTE;
			// _nameTest = "@" + _nameTest;
		} else if (axis.compareTo("parent") == 0) {
			_currAxisType = Constant.PARENT;
		} else if (axis.compareTo("self") == 0) {
			_currAxisType = Constant.SELF;
		} else if (axis.compareTo("ancestor") == 0) {
			_currAxisType = Constant.ANCESTOR;
		} else if (axis.compareTo("ancestor-or-self") == 0) {
			_currAxisType = Constant.ANCESTOR_OR_SELF;
		} else if (axis.compareTo("descendant") == 0) {
			_currAxisType = Constant.DESCENDANT;
		} else if (axis.compareTo("descendant-or-self") == 0) {
			_currAxisType = Constant.DESCENDANT_OR_SELF;
		}
		System.out.println("StepNodes.getAxisType() axisType" + _currAxisType);

		return _currAxisType;
	}

	public String getNameTest() throws NoSuchPathException {
		SimpleNode nodeTestNode;
		String nametest = "";
		if (_currStepNode.jjtGetChild(0).toString().compareTo(
				"AbbrevForwardStep") == 0) {
			nodeTestNode = (SimpleNode) _currStepNode.jjtGetChild(0)
					.jjtGetChild(0);
		} else {
			nodeTestNode = (SimpleNode) _currStepNode.jjtGetChild(1);
		}
		if (nodeTestNode.jjtGetChild(0).toString().compareTo("NameTest") == 0) {
			nametest = ((SimpleNode) nodeTestNode.jjtGetChild(0).jjtGetChild(0))
					.printValue();
		} else if (nodeTestNode.jjtGetChild(0).toString().compareTo(
				"AnyKindTest") == 0) {
			nametest = "node()";
		} else {
			nametest = "not supported yet";
			throw new NoSuchPathException(
					"The NodeTest:KindTest is not supported yet");
		}
		System.out.println("StepNodes.getNameTest(): " + nametest);
		return nametest;
	}

	// public SimpleNode getPredicateList(){
	public PredicateNodes getPredicateList() {
		int i;
		for (i = 0; i < _currStepNode.jjtGetNumChildren(); i++) {
			if (_currStepNode.jjtGetChild(i).toString().compareTo(
					"PredicateList") == 0)
				return new PredicateNodes(_currStepNode.jjtGetChild(i));
		}
		return null;
	}

	public boolean hasPredicate() {
		int i;
		for (i = 0; i < _currStepNode.jjtGetNumChildren(); i++) {
			if (_currStepNode.jjtGetChild(i).toString().compareTo(
					"PredicateList") == 0)
				return true;
		}
		return false;
	}

	public int getNoOfSteps() {
		return _noOfSteps;
	}

	public int getCounter() {
		return _counter;
	}

	public void dump(String prefix) {
		_PathExprNode.dump(prefix);
	}

	public String dumpString(String prefix) {
		String dump = _PathExprNode.dumpString(prefix);
		return dump;
	}

	public void dumpCurrNode(String prefix) {
		_currStepNode.dump(prefix);
	}

	public String dumpStringCurrNode(String prefix) {
		String dump = _currStepNode.dumpString(prefix);
		return dump;
	}
}
